package org.metasketch.simulator.configuration.loaders;

import java.io.File;
import java.io.FileReader;
import java.util.List;
import java.util.Set;

import org.metasketch.demo.model.ActorRole;
import org.metasketch.demo.model.DEMOModel;
import org.metasketch.demo.model.TransactionKind;
import org.metasketch.simulator.petri.PetriNet;
import org.metasketch.simulator.petri.PetriNetSet;
import org.metasketch.simulator.petri.Transition;
import org.metasketch.simulator.resources.Resource;
import org.metasketch.simulator.resources.ResourceBank;
import org.metasketch.simulator.resources.ResourceBase;
import org.metasketch.simulator.resources.ResourceClass;
import org.metasketch.simulator.resources.ResourceRequirements;
import org.metasketch.simulator.transformation.PetriNetSimulationPackage;

import com.thoughtworks.xstream.XStream;

public class DynamicConfigLoader {

	protected static XStream getXStream() {
		XStream xstream = new XStream();
		xstream.autodetectAnnotations(true);

		xstream.processAnnotations(Transaction.class);
		xstream.processAnnotations(SimpleTransaction.class);
		xstream.processAnnotations(DynamicResourceConfig.class);
		// xstream.processAnnotations(GraphicContainer.class);
		// xstream.processAnnotations(GraphicLink.class);
		// xstream.processAnnotations(GraphicNode.class);
		// xstream.processAnnotations(Diagram.class);

		return xstream;
	}

	protected static DynamicResourceConfig load(File file) throws Throwable {

		XStream xstream = getXStream();

		FileReader reader = new FileReader(file);
		DynamicResourceConfig cfg = (DynamicResourceConfig) xstream
				.fromXML(reader);
		reader.close();

		return cfg;

	}

	public static DynamicResourceConfig getDynamicResources(DEMOModel model,
			PetriNetSimulationPackage simulationPackage, File file)
			throws Throwable {
		DynamicResourceConfig cfg = load(file);

		// ResourceBank bank = simulationPackage.getResources();
		PetriNetSet petriNetSet = simulationPackage.getPetriNets();

		List<Transaction> transactions = cfg.getTransactions();

		for (Transaction transaction : transactions) {

			if (transaction instanceof SimpleTransaction) {
				SimpleTransaction sTeam = (SimpleTransaction) transaction;
				String partialKey = sTeam.getId();

				PetriNet petriNet = (PetriNet) petriNetSet
						.getByInitialIdentifier(partialKey);

				if (petriNet == null)
					throw new Exception(
							"No petriNet found with id that starts with: "
									+ partialKey);

				TransactionKind tr = (TransactionKind) model.getIndex()
						.getById(petriNet.getIdentifier());

				ActorRole initiator = tr.getInitiatedBy().iterator().next();

				ResourceBase actor = simulationPackage.getResources()
						.getResourceBaseByIdentifier().get(initiator.getId());

				// List<ResourceClass> internalResourceClass =
				// (List<ResourceClass>) simulationPackage
				// .getResources().getAttributes()
				// .get("internalRoleClasses");

				// if it's one of the internal role classes, it can't be
				// created, needs to be gotten from the pool as they become
				// free...
				// if (internalResourceClass.contains(actor))
				// sTeam.setCreateResource(false);
				// else
				// // this needs to be here since the unmarshalling process puts
				// // it to be the default value of false...which its not..its
				// // defined in the class as true...oh well
				// sTeam.setCreateResource(true);

				sTeam.setActorRoleResource(actor);

			}

		}

		return cfg;

	}

}
